package BiShi.tengxun;

import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        LinkedList<Integer> q = new LinkedList<>();
        while(sc.hasNext()){
            int n = sc.nextInt();
            while(n-- > 0){
                q.clear();
                int M = sc.nextInt();
                sc.nextLine();
                while (M-- > 0){
                    String cop = sc.nextLine();
                    if(cop.contains("PUSH")){
                        q.add(Integer.valueOf(cop.split(" ")[1]));
                    }
                    else if(cop.equals("POP")){
                        if(q.size()==0){
                            System.out.println(-1);
                        }
                        else q.removeFirst();
                    }
                    else if(cop.equals("TOP")){
                        if(q.size()==0){
                            System.out.println(-1);
                        }
                        else System.out.println(q.getFirst());
                    }
                    else if(cop.equals("SIZE")){
                         System.out.println(q.size());
                    }
                    else if(cop.equals("CLEAR")){
                        q.clear();
                    }
                }
            }
        }
    }
}



